<template>
  <div>
    <!-- {{itemval}} -->
    <!-- <searchper :getperlist.sync="perlist"></searchper> -->
    <el-row style="display:flex;">
      <el-input
        placeholder="人员姓名"
        style="width:20%;margin-right:20px"
        v-model="perData.hash.username"
        clearable
      ></el-input>
      <el-select v-model="perData.hash.status" placeholder="审核状态" style="width:20%;margin-right:20px" clearable>
        <el-option
          v-for="item in statusoptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input placeholder="获奖内容" style="width:20%;clear:both" v-model="perData.hash.content" clearable></el-input>
      <el-date-picker
        v-model="perData.hash.grantime"
        style="margin-left:20px;"
        type="daterange"
        rangeSeparator="至"
        startPlaceholder="开始日期"
        endPlaceholder="结束日期"
        valueFormat="yyyy-MM-dd"
        clearable
      ></el-date-picker>

      <el-button style="margin-left:20px;" size="mini" type="primary" @click="getperlist">搜索</el-button>
    </el-row>
    <el-divider></el-divider>
    <el-row>
      <el-button size="mini" @click="showaddper">添加个人业绩</el-button>
      <el-button size="mini" type="primary" @click="amandper">编辑</el-button>
      <el-button size="mini" type="success" @click="deleteper">删除</el-button>
      <el-button size="mini" type="success" @click="checkper">审核</el-button>
    </el-row>
    <el-dialog title="审核个人业绩信息" :visible.sync="dialogVisible1" width="80%">
      <el-form labelWidth="100px">
        <el-form-item label="已选审核人">
          <span v-for="item in itemval" :key="item" style="margin-right:10px">{{item.hash.username}}</span>
        </el-form-item>
        <el-form-item label="审核是否通过">
          <el-radio v-model="status" label="1">审核通过</el-radio>
          <el-radio v-model="status" label="2">审核未通过</el-radio>
        </el-form-item>
        <el-form-item label="原因">
          <el-input v-model="reason" placeholder="原因" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="checkerper">确 定</el-button>
      </span>
    </el-dialog>
    <pertable :tableData="perlist" @itemvals="itemvals" :pagobj.sync="pagobj" :ischeck="false"
      id="tableExcel"
    ></pertable>

    <addper v-if="isaddper" :itemvalname="itemvalname" :chousebtn="btnvalue"></addper>
  </div>
</template>

<script>
import addper from '~/components/person/performance/addper.vue';
import pertable from '~/components/person/performance/pertable.vue';
import FileSaver from 'file-saver';
import XLSX from 'xlsx';

export default {
  data() {
    return {
      status: '1',
      reason: '', // 审核原因
      peridarr: [], // 删除所用的数组id
      isaddper: false, // 添加弹窗
      isaddper2: false, // 编辑弹窗
      btnvalue: 0,
      Object: '',
      itemval: {}, // 编辑传回来的d对象
      id: '',
      dialogVisible: false,
      dialogVisible1: false,
      ts: this.newStore(),
      perData: {
        cols: [],
        hash: {
          username: '',
          content: '',
          grantime: [],
          id: '',
        },
      },
      statusoptions: [{
        label: '待审核',
        value: '0',
      }, {
        label: '审核通过',
        value: '1',
      }, {
        label: '审核不通过',
        value: '2',
      }],
      pagesize: 0,
      currentPage: 0,
      pagobj: {
        // 分页
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },
      perlist: [],
      changelistper: {},
    };
  },
  components: {
    addper,
    pertable,
  },

  created() {
    this.getperlist();
  },
  methods: {
    // 审核确定按钮
    checkerper() {
      const req = {
        handle:
          'D328E4C742B794043F60211ED98E8FE41E274E67F724263D0B54A91C73C5A67C',
        id: this.peridarr,
        status: this.status,
        reason: this.reason,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.$message({
          type: 'success',
          message: '审核提交成功',
        });
        this.getperlist();
        this.dialogVisible1 = false;
      });
    },
    // 审核个人业绩
    checkper() {
      if (this.peridarr.length >= 1) {
        this.reason = '';
        this.status = '1';
        for (let i = 0; i < this.itemval.length; i += 1) {
          if (this.itemval[i].hash.status == '1') {
            this.$message({
              type: 'error',
              message: '已审核通过的数据不能再审核',
            });
            return;
          }
        }
        this.dialogVisible1 = true;
      } else {
        this.$alert('请选择要审核的个人业绩', '提示');
      }
    },
    // 删除个人业绩
    deleteper() {
      // console.log(this.peridarr, "0");
      if (this.peridarr.length > 1) {
        this.$confirm('是否批量删除个人业绩信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
              'D328E4C742B794043F60211ED98E8FE4388F26DA15A6243A479C1A22417B4081',
            id: this.peridarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getperlist();
            }
          });
        });
      } else if (this.peridarr.length == 1) {
        this.$confirm('是否删除该个人业绩信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
              'D328E4C742B794043F60211ED98E8FE4388F26DA15A6243A479C1A22417B4081',
            id: this.peridarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getperlist();
            }
          });
        });
      } else {
        this.$alert('请选择个人业绩', '提示');
      }
    },
    // 编辑个人业绩
    amandper() {
      (this.dialogVisible = true), (this.btnvalue = 2);
      if (Object.keys(this.itemval).length == 1) {
        let itemvals = {};
        itemvals = this.itemval[0];
        this.itemvalname = itemvals;
        // console.log(this.itemvalname, "111111111111");
        this.reload('isaddper');
      } else {
        this.$alert('编辑只能编辑一个', '提示');
      }
    },
    // table 传给父组件
    itemvals(val) {
      // console.log(val, "wwwwww");
      this.itemval = val;
      this.peridarr = [];
      for (let i = 0; i < this.itemval.length; i++) {
        this.peridarr.push(this.itemval[i].hash.id);
      }
      // console.log(this.peridarr, "23");
    },
    // 监听子组件的(添加)
    addper(val) {
      this.Object = {};
      this.Object = val;
      // val.hash.holder = val.hash.holders;
      // console.log(val, "22223322");
      // this.carlist.unshift(val);
      // console.log(this.perlist, "66666");
    },
    // 公共组件刷新
    reload(partsname) {
      // console.log(partsname);
      this[partsname] = false;
      this.$nextTick(() => {
        this[partsname] = true;
      });
    },
    // 分页
    handleCurrentChange() {
      this.getlaborlist();
    },
    // 点击添加个人业绩信息
    showaddper() {
      (this.dialogVisible = true), (this.btnvalue = 1);
      this.reload('isaddper');
    },
    getperlist() {
      // console.log("2222");
      if (this.perData.hash.grantime == null) {
        this.perData.hash.grantime = [];
      }
      const req = {
        handle:
          'D328E4C742B794043F60211ED98E8FE473936C62493B31D1F6486B1D96ED9F7E',
        username: this.perData.hash.username,
        content: this.perData.hash.content,
        grantime: this.perData.hash.grantime[0],
        grantimetwo: this.perData.hash.grantime[1],
        status: this.perData.hash.status,
        id: this.perData.hash.id,
        start: (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize,
        limit: this.pagobj.pagesize,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.perlist = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
        // console.log(this.perlist, "789");
      });
    }
  },
};
</script>

